{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ffeb4eee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "import sys\n",
    "\n",
    "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
    "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1a9eb90-335c-4214-8bb6-fd1edbe3ccbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# My OpenAI Key\n",
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"INSERT OPENAI KEY\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8d0b2364-4806-4656-81e7-3f6e4b910b5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index import TreeIndex, SimpleDirectoryReader\n",
    "from IPython.display import Markdown, display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1298bbb4-c99e-431e-93ef-eb32c0a2fc2a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Building index from nodes: 9 chunks\n",
      "0/95\n",
      "10/95\n",
      "20/95\n",
      "30/95\n",
      "40/95\n",
      "50/95\n",
      "60/95\n",
      "70/95\n",
      "80/95\n",
      "90/95\n",
      "> [build_index_from_documents] Total token usage: 34226 tokens\n"
     ]
    }
   ],
   "source": [
    "documents = SimpleDirectoryReader(\"data\").load_data()\n",
    "new_index = TreeIndex.from_documents(documents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "68c9ebfe-b1b6-4f4e-9278-174346de8c90",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Starting query: What did the narrator do after getting back to Chicago?\n",
      ">[Level 0] Selected node: [8]/[8]\n",
      ">[Level 1] Selected node: [8]/[8]\n",
      "> [query] Total token usage: 6058 tokens\n"
     ]
    }
   ],
   "source": [
    "# set Logging to DEBUG for more detailed outputs\n",
    "\n",
    "query_engine = new_index.as_query_engine()\n",
    "response = query_engine.query(\"What did the narrator do after getting back to Chicago?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "91581e60-6051-40ae-bba6-8fa08ffbb728",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The narrator returned to his home in Chicago and began calling people to inform them of Gatsby's funeral. He was worried that the funeral would draw a sightseeing crowd and wanted to keep it private. He was relieved when Klipspringer called and promised to tell anyone who might be interested about the funeral. He then asked Klipspringer to commit to attending the funeral, but Klipspringer hesitated.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca10a9c1-9dff-476d-b218-3208a1b8e7f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# GPT is confused by the text evidence\n",
    "query_engine = new_index.as_query_engine()\n",
    "response = query_engine.query(\"What did Gatsby do before he met Daisy?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fc3f18a-0ef9-453c-acf8-7aedd784cdcf",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
